package com.ynet.h2.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ynet.excetion.PlatformErrorCode;
import com.ynet.utils.DateUtils;

/**
 * 请求数据
 * @author liuxin
 * 2018年1月30日
 */
public class ReqData {
	private final static Logger logger = LoggerFactory.getLogger(ReqData.class);

	/**
	 * 保存外网请求数据
	 *	ID                   varchar(32) not null comment 'ID',
	 *  	appid                varchar(20) comment 'APPID',
	 *  	data_type            varchar(5) comment '数据类型 json form',
	 *  	req_data             varchar(5000) comment '请求数据',
	 *  	wan_url              varchar(5000) comment '外网链接',
	 *  lan_url              varchar(5000) comment '内网链接',
	 *  req_id               int comment '请求批次',
	 *  req_time             date comment '请求时间',
	 *	get_time             date comment '获取时间',
	 *	method               get  post  请求方法
	 */
	public static void saveReqData(String appid, String data_type, String req_data, String wan_url, String method, String lanUrl, String type) {
		
		Statement stmt = null;
		Connection conn = null;
		

		try{
			conn = new ConnectionPool().getConnection();
			stmt = conn.createStatement();
			int count = stmt.executeUpdate("INSERT INTO req_data(id,appid,data_type,req_data,req_time, method, lan_url, req_type ) VALUES"
					+ "('" + UUID.randomUUID() + "','" + appid + "','" + data_type + "','" + req_data + "','"
					+ DateUtils.getCurrentDate() + "','" + method + "','" + lanUrl + "','" + type +"')");
			logger.debug("更新行 ： " + count);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 获得外网请求数据
	 * appid 
	 * type 类型1-外网请求、2-内网响应
	 */
	public static Map<String, String> findAppId(String appid, String type) {

		Map<String, String> dataMap = new Message().getMessage();
		Statement stmt = null;
		Connection conn = null;
		System.out.println("------------------------------");
		try {
			conn = new ConnectionPool().getConnection();
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM req_data where appid=" + "'" + appid + "' AND req_type=" + "'" + type + "'");
			// 遍历结果集
			while (rs.next()) {
				dataMap.put("id", rs.getString("id"));
				dataMap.put("appid", rs.getString("appid"));
				dataMap.put("data_type", rs.getString("data_type"));
				dataMap.put("req_data", rs.getString("req_data"));
				dataMap.put("method", rs.getString("method"));
				dataMap.put("lan_url", rs.getString("lan_url"));
			}
			logger.debug(dataMap.toString());

			if(dataMap.isEmpty()){
				dataMap = new Message().getMessage(PlatformErrorCode.error3, "查询记录为空");
			}else{
				dataMap.put("returnCode", "000000");
				dataMap.put("returnMsg", "成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null) stmt.close();
				if (conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return dataMap;
	}
	
	/**
	 * 删除数据
	 */
	public static boolean delReqDataByAppIdAndType(String appid, String type) {
		Statement stmt = null;
		Connection conn = null;
		boolean count = false;
		try {
			conn = new ConnectionPool().getConnection();
			stmt = conn.createStatement();
			String delSql = "delete from req_data where appid=" + "'" + appid + "' and req_type=" + "'" + type + "'";
			count = stmt.execute(delSql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null) stmt.close();
				if (conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}
	
	
}
